package misc
{
	import mx.controls.DateField;

	public class c_DATE
	{
		public function c_DATE()
		{
		}
		
		public function date_to_string(DATE_date:Date,FORMAT:String = 'DD.MM.YYYY',TIME:Boolean = false):String
		{
			var STRING_result:String = '';
			// Just BTW: Flex assigns 0 to January, 1 to February, 2 to March, ...
			if(DATE_date != null)
			{
				if(FORMAT == 'DD.MM.YYYY')
				{
					// trace(DATE_date.toDateString()); // Fri Sep 12 2008
					
					// trace('UTC:'+DATE_date.dateUTC.toString()+'.'+(DATE_date.monthUTC+1).toString()+'.'+DATE_date.fullYear); // 12.9.2008
					
					/** I PREFER THIS OVER UTC, because when using
					 * 	var DATUM_result:Date = c_DATE_inst.string_to_date(STRING_Datum,STRING_DateFormat);
					 the UTC values are not set correctly. **/
					
					// trace('   :'+DATE_date.date.toString()+'.'+(DATE_date.month+1).toString()+'.'+DATE_date.fullYear); // 12.9.2008
					
					var STRING_month:String = (DATE_date.month+1).toString();
					
					if(STRING_month.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_month = '0'+STRING_month
					}
					var STRING_day:String = DATE_date.date.toString();
					
					if(STRING_day.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_day = '0'+STRING_day
					}
					
					STRING_result = STRING_day+'.'+STRING_month+'.'+DATE_date.fullYear;
					// trace(STRING_result); // 12.09.2008
				}
				
				if(FORMAT == 'YYYY-MM-DD')
				{
					var STRING_month:String = (DATE_date.month+1).toString();
					if(STRING_month.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_month = '0'+STRING_month
					}
					var STRING_day:String = DATE_date.date.toString();
					if(STRING_day.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_day = '0'+STRING_day
					}
					STRING_result = DATE_date.fullYear+'-'+STRING_month+'-'+STRING_day;
					// trace(STRING_result); // 12.09.2008
				}
				if(FORMAT == 'YYYY.MM.DD')
				{
					var STRING_month:String = (DATE_date.month+1).toString();
					if(STRING_month.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_month = '0'+STRING_month
					}
					var STRING_day:String = DATE_date.date.toString();
					if(STRING_day.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_day = '0'+STRING_day
					}
					STRING_result = DATE_date.fullYear+'.'+STRING_month+'.'+STRING_day;
					// trace(STRING_result); // 12.09.2008
				}
				if(FORMAT == 'YYYY:MM:DD')
				{
					var STRING_month:String = (DATE_date.month+1).toString();
					if(STRING_month.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_month = '0'+STRING_month
					}
					var STRING_day:String = DATE_date.date.toString();
					if(STRING_day.length == 1) // ein ziffriger monat hänge ne null hin
					{
						STRING_day = '0'+STRING_day
					}
					STRING_result = DATE_date.fullYear+':'+STRING_month+':'+STRING_day;
					// trace(STRING_result); // 12.09.2008
				}
			}
			
			if(TIME)
			{
				STRING_result = STRING_result + " " + time(DATE_date);
			}
			
			return STRING_result;
		}
		
		/** input a date returns time-string in format HH:MM:SS **/
		public function time(DATE:Date):String
		{
			var STRING_result:String = DATE.toTimeString();
			// remove  GMT+0200 from STRING_result = "01:00:08 GMT+0100"
			
			STRING_result = STRING_result.substring(0,8); // DATE.getHours()+":"+DATE.getMinutes()+":"+DATE.getSeconds();
			
			return STRING_result;
		}
		
		/** time from milliseconds **/
		public function countTime(MSECONDS:Number):String
		{
			return SecondsToYearMonthDayHoursMinutesSeconds(MSECONDS*1000);
		}

		public function string_to_date(STRING_Datum:String,STRING_DateFormat:String = 'DD.MM.YYYY'):Date // STRING_DateFormat = 'DD.MM.YYYY'
		{
			var DATUM_result:Date = null;
			if(STRING_Datum != null)
			{
				DATUM_result = DateField.stringToDate(STRING_Datum,STRING_DateFormat);
				/** setz dateUTC nicht korrekt **/
			}
			
			return DATUM_result;
		}
		
		public function string2date(STRING_Datum:String,STRING_DateFormat:String = 'DD.MM.YYYY'):Date // STRING_DateFormat = 'DD.MM.YYYY'
		{
			return string_to_date(STRING_Datum,STRING_DateFormat);
		}
		
		public static const EXACT_SECONDS_IN_YEAR:uint = 	31556926; 	// according to Google
		public static const EXACT_SECONDS_IN_MONTH:Number =	2629743.83;	// according to Google
		public static const EXACT_SECONDS_IN_DAY:uint =		86400;		// according to Google
		
		public static const SECONDS_IN_MONTH:Number =		60 * 60 * 24 * 30.4167;// non leap year
		public static const SECONDS_IN_DAY:uint = 			60 * 60 * 24;//	86400;
		public static const SECONDS_IN_YEAR:uint = 			SECONDS_IN_DAY * 365;
		
		public static const MILLISECONDS_IN_MINUTE:int = 	1000 * 60;
		public static const MILLISECONDS_IN_HOUR:int = 		1000 * 60 * 60;
		public static const MILLISECONDS_IN_DAY:int = 		1000 * 60 * 60 * 24;

		/**
		 * adobe flex as3 actionscript how to convert 1237891273 seconds into 39 years 2 months 23 days 10 hours 41 minutes and 13 seconds.
		 * Dan Florio, aka: polyGeek
		 * http://polygeek.com/flex/513_ConvertSecondsToEnglish/srcview/index.html
		**/
		public function SecondsToYearMonthDayHoursMinutesSeconds( n:Number ):String
		{
			n *= 1000;
			
			var d:String = "";
			var and:String;
			var time:Date = new Date( n );
			
			var years:uint = time.fullYearUTC - 1970;
			var months:uint = time.monthUTC;
			var days:uint = time.dateUTC - 1;
			var hours:uint = time.hoursUTC;
			var mins:uint = time.minutesUTC;
			var secs:uint = time.secondsUTC;
			
			// years	
			if( years > 0 && ( months > 0 || days > 0 || hours > 0 || mins > 0 || secs > 0 ) ) {
				d = ( years > 1 ) ? 
					String( years ) + " years" : 
					"1 year";
			} else if( years > 0 ){
				d = ( years > 1 ) ? 
					String( years ) + " years." : 
					"1 year.";
			}
			
			// months
			if( months > 0 && ( days > 0 || hours > 0 || mins > 0 || secs > 0 ) ) {	
				d = insertEndSpace( d );
				d += ( months > 1 ) ?
					String( months ) + " months" :
					"1 month";
			} else if( months > 0 ) {
				and = ( d.length > 0 ) ? " and " : "";
				d += ( months > 1 ) ?
					and + String( months ) + " months" :
					and + "1 month.";
				
			}
			
			// days
			if( days > 0 && ( hours > 0 || mins > 0 || secs > 0 ) ) {
				d = insertEndSpace( d );
				d += ( days > 1 ) ?
					String( days ) + " days" :
					"1 day";
			} else if( days > 0 ) {
				and = ( d.length > 0 ) ? " and " : "";
				d += ( days > 1 ) ?
					and + String( days ) + " days" :
					and + "1 day.";
			}
			
			// hours
			if( hours > 0 && ( mins > 0 || secs > 0 ) ) {
				d = insertEndSpace( d );
				d += ( hours > 1 ) ?
					String( hours ) + " hours" :
					"1 hour";
			} else if( hours > 0 ) {
				and = ( d.length > 0 ) ? " and " : "";
				d += ( hours > 1 ) ?
					and + String( hours ) + " hours" :
					and + "1 hour.";				
			}
			
			// minutes
			if( mins > 0 && secs > 0 ) {
				d = insertEndSpace( d );
				d += ( mins > 1 ) ?
					String( mins ) + " minutes" :
					"1 minute";
			} else if( mins > 0 ) {
				and = ( d.length > 0 ) ? " and " : "";
				d += ( mins > 1 ) ?
					and + String( mins ) + " minutes" :
					and + "1 minute.";
			}
			
			// seconds	
			if( secs > 0 && d.length > 0 ) {
				d += ( secs > 1 ) ?
					" and " + String( secs ) + " seconds." :
					" and 1 second.";
			} else if( secs > 0 ) {
				d = insertEndSpace( d );
				d += ( secs > 1 ) ?
					String( secs ) + " seconds." :
					"1 second.";
			}
			
			if(n == 0)
			{
				d = "0 seconds";
			}
			
			return d;
		}
		
		private static function insertEndSpace( s:String ):String {
			s += ( s.length > 0 ) ? " " : "";
			return s;
		}
	}
}
